<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .modal {
      position: fixed;
      top: 50%;
      left: 50%;
      width: 500px;
      height: 500px;
      margin: -250px 0 0 -250px;
      box-shadow: 1px 3px 5px #999;
    }

    .modal header {
      height: 50px;
      background-color: #ccc;
      color: #fff;
      padding: 0 15px;
      box-sizing: border-box;
      font-size: 15px;
      line-height: 50px;
    }

    .modal.success header {
      background-color: green;
    }

    .modal.warning header {
      background-color: orange;
    }

    .modal.error header {
      background-color: red;
    }
  </style>
</head>

<body>
  <div class="wrap">
    <div class="modal">
      <header>Modal</header>
    </div>
    <div class="btn-group">
      <button data-status="S">成功</button>
      <button data-status="W">告警</button>
      <button data-status="E">失败</button>
    </div>
  </div>
</body>
<script>
  // factory 

  /**
   * 有一些公共的方法，属性，静态工具
   * modal 父类
   * 
   * 对每种状态内部属性加工，或者是每种状态的功能的扩展
   * success warning error 三个不通的类去完成
   * 
   * 有个工厂通过传入的状态不通给我自动化实例化相应的类型
  */
  class Modal {
    constructor(status) {
      this.status = status
    }
    get ClassName() {  
      let classStr = 'modal '
      switch (this.status) {
        case "S":
          classStr += 'success'
          break
        case "W":
          classStr += 'warning'
          break;
        case "E":
          classStr += 'error'
          break;
        default:
          break;
      }
      return classStr
    }
  }

  const oModal = document.getElementsByClassName("modal")[0]
  const oBtnGroup = document.getElementsByClassName("btn-group")[0]

  // 实例化操作dom
  const modalFactory = new ModalFactory(oModal)
  function init() {
    function bindEvent() {
      oBtnGroup.addEventListener("click", handleBtnClick, false)
    }
    function handleBtnClick(e) {
      const target = e.target
      const tagName = target.tagName.toLowerCase()
      if (tagName === 'button') {
        const status = target.dataset.status
        // chageStatus(status)
        modalFactory.create("这是一个工厂模式的场景", status)
      }
    }
    function chageStatus(status) {
      switch (status) {
        case "S":
          oModal.className = 'modal success'
          break;
        case "W":
          oModal.className = 'modal warning'
          break;
        case "E":
          oModal.className = 'modal error'
          break;
        default:
          break;
      }
    }
    bindEvent()

  }
  init()

</script>

</html>